﻿///<reference path="../jquery-1.4.1.min.js" />

var review_element;
var review_productId;

function displayReview(element, productId, pageIndex) {
    review_element = element ? element : review_element;
    review_productId = productId ? productId : review_productId;
    pageIndex = pageIndex ? pageIndex : 1;
    var url = "/Products/Review/" + question_productId + "/" + pageIndex + "?r=" + Math.random();
    $.get(
        url,
        null,
        function (data, status) {
            var html = "";
            if (data.Items && data.Items.length > 0) {
                for (var i in data.Items) {
                    html += initial_ReviewItem(data.Items[i]);
                }
                html += initial_ReviewPager(review_productId, pageIndex, data.PageSize, data.Total);
            }
            else {
                html = "<div class=\"content\">There are no reviews for this product.</div>";
            }
            review_element.html(html);
        },
        "json");
    }

    var review_item_template = "<div class=\"content\">"
                        + "<strong class=\"author\">{0}</strong>"
                        //+ "<img src=\"/images/stars_{1}.png\" alt=\"{1} Point\" />"
                        + "{1}"
                        + "<span class=\"date\">{2}</span>"
                        + "<p>{3}</p>"
                        + "</div>";

    function initial_ReviewItem(data) {
        return $.format(review_item_template, data.Name, initial_ReviewRate(data.Rate), data.CreateDate, data.Remark);
    }


    function initial_ReviewPager(productId, cPageIndex, pageSize, total) {
        var html = " <div class=\"pagination\">";
        if (cPageIndex > 1) {
            html += "<a href='javascript:void(0);' onclick='javascript:displayReview(null," + productId + "," + (cPageIndex - 1) + ");'>Previous</a>";
        }
        else {
            html += "<a>Previous</a>";
        }
        var totalPageNumber = parseInt(total / pageSize) + (total % pageSize > 0 ? 1 : 0);
        for (var i = 1; i <= totalPageNumber; i++) {
            if (i == cPageIndex) {
                html += "<span class='current'>" + i + "</span>";
            } else {
                html += "<a href='javascript:void(0)' onclick='javascript:displayReview(null," + productId + "," + i + ");'>" + i + "</a>";
            }
        }
        if (cPageIndex < totalPageNumber) {
            html += "<a href='javascript:void(0);' onclick='javascript:displayReview(null," + productId + "," + (cPageIndex + 1) + ");'>Next</a>";
        } else {
            html += "<a>Next</a>";
        }
        html += "</div>";
        return html;
    }

    var rate_Container = '<span class="star-rating-control">{0}</span>';
    var rate_template = '<div class="star-rating rater-0 star star-rating-live {0}"><a title="{1}">{1}</a></div>';
    function initial_ReviewRate(rate) {
        var rate_content = "";
        if (rate) {
            for (var i=1; i<=5;i++){
                var enableClass = "";
                if (i <= rate) {
                    enableClass = "star-rating-on";
                }
                rate_content += $.format(rate_template, enableClass, i);
            }
            return $.format(rate_Container, rate_content);
        }
        return "";
    }